package au.com.bluelink.scjp.chap2;

public class CastTest {

	public static void main(String[] args) {

		Animal[] a = { new Animal(), new Dog(), new Animal() };
		
		// TODO: Java 5 use "for(Animal animal : a)"
		
		for (int i = 0; i < a.length; i++) {

			Animal animal = a[i];
			animal.makeNoise();

			if (animal instanceof Dog) {
				Dog d = (Dog) animal; // casting the ref. var.
				d.playDead();
			}

		}
	}
}

class Animal {
	void makeNoise() {
		System.out.println("generic noise");
	}
}

class Dog extends Animal {
	void makeNoise() {
		System.out.println("bark");
	}

	void playDead() {
		System.out.println(" roll over");
	}
}